package vn.pyco.prez.tapestry.services;

import java.util.List;

import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.ServiceBinder;
import org.apache.tapestry5.ioc.annotations.ServiceId;
import org.apache.tapestry5.ioc.annotations.SubModule;
import org.apache.tapestry5.services.javascript.JavaScriptStack;
import org.cometd.bayeux.server.ServerSession;

import vn.pyco.prez.PrezConstants;
import vn.pyco.prez.dao.DaoManager;
import vn.pyco.prez.dao.impl.InMemoryDaoManager;
import vn.pyco.prez.model.Slide;
import vn.pyco.prez.tapestry.services.comet.BayeuxModule;
import vn.pyco.prez.tapestry.services.comet.CometJavaScriptStack;
import vn.pyco.prez.tapestry.services.internal.AppHelperImpl;
import vn.pyco.prez.tapestry.services.internal.ThreadSourceImpl;
import vn.pyco.prez.tapestry.tasks.ConvertionTask;

/**
 * This module is automatically included as part of the Tapestry IoC Registry, it's a good place to
 * configure and extend Tapestry, or to place your own service definitions.
 */
@SubModule(BayeuxModule.class)
public class AppModule {
	
    public static void contributeApplicationDefaults(MappedConfiguration<String, String> configuration) {
        
        configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en");

        configuration.add(SymbolConstants.PRODUCTION_MODE, "false");

        //configuration.add(SymbolConstants.APPLICATION_VERSION, "4.0.0-SNAPSHOT");
        
    }	

	public static void bind(ServiceBinder binder) {
        binder.bind(AppHelper.class, AppHelperImpl.class);
        binder.bind(ThreadSource.class, ThreadSourceImpl.class);
    }    
    
	public static void contributeFactoryDefaults(MappedConfiguration<String, String> configuration) {
        configuration.add("prez.assets", "vn/pyco/prez/tapestry/assets");
        configuration.add("prez.scripts", "classpath:${prez.assets}/js");

    }	

	public static void contributeJavaScriptStackSource(MappedConfiguration<String, JavaScriptStack> configuration) {
		 configuration.addInstance(PrezConstants.COMETD_JSSTACK, CometJavaScriptStack.class);
	}
	
	@ServiceId("taskManager")
	public static DaoManager<ConvertionTask> buildTaskManager() {
		return new InMemoryDaoManager<ConvertionTask>();
	}
	
	@ServiceId("clientSessionManager")
	public static DaoManager<ServerSession> buildClientSessionManager() {
		return new InMemoryDaoManager<ServerSession>();
	}
	
	@ServiceId("slideManager")
	public static DaoManager<List<Slide>> buildSlideManager() {
		return new InMemoryDaoManager<List<Slide>>();
	}
	
}
